/**
 *chongzhi
 */

module game {
    export class CZ_XianXiaSaoMaPanel extends eui.Component {
        public page1: eui.Group;
        public title: eui.Image;
        public erweima: eui.Image;
        public textGroup: eui.Group;
        public zhanghaoGroup: eui.Group;
        public zhanghao: eui.Label;
        public selectGroup: eui.Group;
        public next: eui.Button;
        public page2: eui.Group;
        public time: eui.Label;
        public moneyNum: eui.EditableText;
        public addButtonGroup: eui.Group;
        public back: eui.Button;
        public conmitButton: eui.Button;



        private data = null;
        private addmoney = null;//选中的金额
        private PayId = null;//渠道ID
        private Addr = null;
        private MinAmt = null;
        private MaxAmt = null;

        private sprite: egret.Sprite;
        public constructor(data: Object) {
            super();
            this.data = data;
            this.addEventListener(egret.Event.COMPLETE, this.init, this)
            // this.addEventListener(egret.Event.REMOVED, this.destroy, this)
            this.skinName = "resource/ui/game/CZ_OfflinePaySaoMa.exml";
        }
        private destroy() {
            var leng = this.addButtonGroup.numChildren
            for (var i = 0; i < leng; i++) {
                this.addButtonGroup.getChildAt(i).removeEventListener(egret.TouchEvent.TOUCH_TAP, this.addButtonClick, this)
            }
            var leng = this.selectGroup.numChildren
            for (var i = 0; i < leng; i++) {
                this.selectGroup.getChildAt(i).removeEventListener(egret.TouchEvent.TOUCH_TAP, this.radioButtonClic, this)
            }
            this.next.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.pageChange, this)
            this.back.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.pageChange, this)
            this.zhanghaoGroup.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.zhanghaoClick, this)
            this.conmitButton.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.conmitClick, this)
            this.moneyNum.removeEventListener(egret.FocusEvent.FOCUS_IN, () => {
                this.moneyNum.text = "";
            }, this)
            this.moneyNum.removeEventListener(egret.FocusEvent.FOCUS_OUT, () => {
                var text = parseInt(this.moneyNum.text)
                if (text + "" != "NaN") {
                    this.addmoney = text
                    this.moneyNum.text = text + ""
                } else {
                    this.moneyNum.text = ""
                    this.addmoney = null
                }
            }, this)
            this.time.removeEventListener(egret.TouchEvent.TOUCH_TAP, (eve: egret.TouchEvent) => {
                openCalendar(this.time)
            }, this)
            this.time.parent.removeEventListener(egret.TouchEvent.TOUCH_TAP, (eve: egret.TouchEvent) => {
                openCalendar(this.time)
            }, this)
        }
        private init() {
            //初始化UI显示
            var PayType = this.data["PayType"];
            var list: Array<Object> = this.data["list"];
            var TransferType = list[0]["TransferType"]
            if (PayType == "OfflineZhiFuBao") {//支付宝
                this.title.source = "chongzhi_zfbsk"
            } else if (PayType == "OfflineWeiXin") {//微信
                this.title.source = "chongzhi_wxsk"
            }
            //生成多选栏
            var leng = list.length
            var y = 0;
            for (var i = 0; i < leng; i++) {
                var radioButton = new eui.RadioButton();
                radioButton.label = list[i]["PayName"];
                radioButton.name = list[i]["PayId"];
                radioButton.groupName = "1";
                radioButton.x = 50;
                radioButton.y = y
                radioButton.addEventListener(egret.TouchEvent.TOUCH_TAP, this.radioButtonClic, this)
                this.selectGroup.addChild(radioButton);
                y += 40
                //自动选中第一个
                if (i == 0) {
                    radioButton.selected = true
                    var eve = new egret.TouchEvent(egret.TouchEvent.TOUCH_TAP);
                    eve.$currentTarget = radioButton;
                    this.radioButtonClic(eve)
                }
            }
            //切换收款人
            this.zhanghaoGroup.addEventListener(egret.TouchEvent.TOUCH_TAP, this.zhanghaoClick, this)
            //翻页事件
            this.next.addEventListener(egret.TouchEvent.TOUCH_TAP, this.pageChange, this)
            this.back.addEventListener(egret.TouchEvent.TOUCH_TAP, this.pageChange, this)
            this.conmitButton.addEventListener(egret.TouchEvent.TOUCH_TAP, this.conmitClick, this)
            this.moneyNum.addEventListener(egret.FocusEvent.FOCUS_IN, () => {
                this.moneyNum.text = "";
            }, this)
            this.moneyNum.addEventListener(egret.FocusEvent.FOCUS_OUT, () => {
                var text = parseInt(this.moneyNum.text)
                if (text + "" != "NaN") {
                    this.addmoney = text
                    this.moneyNum.text = text + ""
                } else {
                    this.moneyNum.text = ""
                    this.addmoney = null
                }
            }, this)
            this.time.addEventListener(egret.TouchEvent.TOUCH_TAP, (eve: egret.TouchEvent) => {
                openCalendar(this.time)
            }, this)
            this.time.parent.addEventListener(egret.TouchEvent.TOUCH_TAP, (eve: egret.TouchEvent) => {
                openCalendar(this.time)
            }, this)
        }

        //点击账号切换弹出下拉选项
        private zhanghaoClick(eve: egret.TouchEvent) {
            EffectUtils.playDim(eve.currentTarget)
            if (this.selectGroup.visible == true) {
                this.selectGroup.visible = false;
            } else {
                this.selectGroup.visible = true;
            }
        }
        //翻页
        private pageChange(eve: egret.TouchEvent) {
            if (eve.currentTarget == this.next) {
                this.page1.visible = false
                this.page2.visible = true
                var now = new Date();
                var year = now.getFullYear(); //得到年份
                var month: string = (now.getMonth() + 1).toString();//得到月份
                if (month.length == 1) month = "0" + month
                var date = (now.getDate()).toString();//得到日期
                if (date.length == 1) date = "0" + date
                var hour = (now.getHours()).toString();//得到小时
                if (hour.length == 1) hour = "0" + hour
                var minu = (now.getMinutes()).toString();//得到分钟
                if (minu.length == 1) minu = "0" + minu
                this.time.text = year + "-" + month + "-" + date + " " + hour + ":" + minu;
            } else {
                this.page1.visible = true
                this.page2.visible = false
            }
        }
        //选择账号
        private radioButtonClic(eve: egret.TouchEvent) {
            this.selectGroup.visible = false;
            var radioButton = <eui.RadioButton>eve.currentTarget;
            var obj = null;
            var list: Array<Object> = this.data["list"];
            var leng = list.length
            for (var i = 0; i < leng; i++) {
                if (list[i]["PayId"] == radioButton.name) {
                    obj = list[i];
                    break;
                }
            }
            if (obj) {
                this.zhanghao.text = obj["PayName"];
                //生成二维码
                if (this.page1.contains(this.sprite)) {
                    this.page1.removeChild(this.sprite)
                    this.sprite = null;
                }
                this.sprite = qr.QRCode.create(obj.PayServiceURL);
                this.sprite.x = this.erweima.x
                this.sprite.y = this.erweima.y
                this.page1.addChildAt(this.sprite, 1)

                this.PayId = obj["PayId"];
                this.Addr = obj["CheckAddr"];
                this.MinAmt = obj["MinAmt"];
                this.MaxAmt = obj["MaxAmt"];
                this.moneyNum.prompt = "最低充值" + this.MinAmt + "元"
                this.textGroup.removeChildren()
                var text = new egret.TextField()
                text.textFlow = [
                    { text: "单笔最低充值:", style: { textColor: 0xBCB1B1, fontFamily: "Microsoft YaHei" } },
                    { text: this.MinAmt, style: { textColor: 0xffce24, fontFamily: "Microsoft YaHei" } },
                    { text: '元，最高充值:', style: { textColor: 0xBCB1B1, fontFamily: "Microsoft YaHei" } },
                    { text: this.MaxAmt, style: { textColor: 0xffce24, fontFamily: "Microsoft YaHei" } },
                    { text: '元。', style: { textColor: 0xBCB1B1, fontFamily: "Microsoft YaHei" } }
                ]
                this.textGroup.addChild(text)
                var Moneys: Array<number> = obj["Moneys"]
                if (Moneys.length == 0) {
                    Moneys = [50, 100, 500, 1000, 5000, 10000, 50000, 100000];//后端提供充值金额就用后端的，没有就用前端默认的
                }
                //充值数额按钮
                var leng = this.addButtonGroup.numChildren
                for (var i = 0; i < leng; i++) {
                    this.addButtonGroup.getChildAt(i).removeEventListener(egret.TouchEvent.TOUCH_TAP, this.addButtonClick, this)
                }
                this.addButtonGroup.removeChildren()
                for (var i = 0; i < Moneys.length; i++) {
                    var moneyButton = new components.moneyButton();
                    moneyButton.icon = "chongzhi_money_bg"
                    moneyButton.wenben.text = Moneys[i] + ""
                    this.addButtonGroup.addChild(moneyButton)
                    moneyButton.addEventListener(egret.TouchEvent.TOUCH_TAP, this.addButtonClick, this)
                }
            }
        }
        //点击选金币按钮
        private addButtonClick(eve: egret.TouchEvent) {
            var button: components.moneyButton = eve.currentTarget;
            this.addmoney = button.getShuzhi()
            this.moneyNum.text = button.getShuzhi();
            this.moneyNum.textColor = 0xEFC851;
        }
        //点击快捷支付充值提交按钮
        private conmitClick(eve: egret.TouchEvent) {
            var playtype = this.PayId;
            var addmoney = this.addmoney;
            if (playtype && addmoney) {
                if (addmoney < this.MinAmt) {
                    EffectUtils.textFadeAway("最少充值" + this.MinAmt + "元!")
                    return;
                }
                if (addmoney > this.MaxAmt) {
                    EffectUtils.textFadeAway("最多充值" + this.MaxAmt + "元!")
                    return;
                }
                var time = Global.getCurrTimeLong()
                var key = Global.getSynLoginKey(time);
                var saveTimeStamp = this.time.text;
                var date = new Date(saveTimeStamp);
                var ceateTime = date.getTime() / 1000;

                var request = new egret.HttpRequest();
                request.responseType = egret.HttpResponseType.TEXT;
                request.open(InstructionsEnum.IP_API + this.Addr + "?userid=" + GameVO.getVo().dwUserID + "&time=" + time + "&signature=" + key + "&payId=" + playtype + "&amount=" + addmoney + "&saveTimeStamp=" + ceateTime + "&userRealName=", egret.HttpMethod.GET);
                request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                request.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
                    EffectUtils.hideWaritPanel()
                    var request = <egret.HttpRequest>event.currentTarget;
                    try {
                        var obj = JSON.parse(request.response)
                        // if (obj.code == 0) {
                        Global.tipShow(obj.msg)
                        // } else {
                        //     Global.tipShow(obj.msg)
                        // }
                    } catch (err) {
                        EffectUtils.textFadeAway("数据包异常，无法解析!")
                    }
                }, this);
                request.addEventListener(egret.IOErrorEvent.IO_ERROR, () => {
                    EffectUtils.hideWaritPanel()
                    Global.tipShow("网络请求失败，请稍后重试!")
                }, this);
                request.send();
                EffectUtils.showWaritPanel()

            }
        }
    }
}